const router = require('koa-router')()
const db = require('../db/initdb')

//查看粉丝和关注状态
router.post("/fan", async (ctx, next) => {
  let type = ctx.query.type
  let userid = ctx.request.body.userid
  let concernid = ctx.request.body.concernid
  //粉丝列表
  if (type == 1) {
    let sql1 = "select u.id,u.avatar,u.name,u.intro,c1.hu from hs.concern AS c1,hs.user AS u where (c1.userid=$1 AND (c1.hu=2 OR c1.hu=0) AND c1.concernid=u.id) OR (c1.concernid=$1 AND (c1.hu=1) AND c1.userid=u.id)"
    let args = [userid]
    let ret1 = await db.query(sql1, args);
    ctx.body = { "status": 200, "concern": ret1.rows }
  }

  //关注列表
  if (type == 2) {
    let sql1 = "select u.id,u.avatar,u.name,u.intro,c1.hu from hs.concern AS c1,hs.user AS u where (c1.userid=$1 AND (c1.hu=1 OR c1.hu=0) AND c1.concernid=u.id) OR (c1.concernid=$1 AND (c1.hu=2) AND c1.userid=u.id)"
    let args = [userid]
    let ret1 = await db.query(sql1, args);
    ctx.body = { "status": 200, "concern": ret1.rows }
  }
  //查看关注状态
  if (type == 3) {
    let sql1 = "select hu,userid from hs.concern where (userid=$1 AND concernid=$2) OR (userid=$3 AND concernid=$4)"
    let args = [userid, concernid, concernid, userid]
    let ret1 = await db.query(sql1, args);
    //未关注状态
    if (ret1.rowCount <= 0) {
      ctx.body = { "status": 200, "hu": "false" }
    }
    if (!ret1.rows[0]) {
      console.log('a')
      ctx.body = { "status": 200, "hu": false }
      return
    }
    if (ret1.rows[0].hu == 2 && userid == ret1.rows[0].userid) {
      ctx.body = { "status": 200, "hu": "false" }
    }
    if (ret1.rows[0].hu == 1 && concernid == ret1.rows[0].userid) {
      ctx.body = { "status": 200, "hu": "false" }
    }
    //关注状态
    if (ret1.rows[0].hu == 0) {
      ctx.body = { "status": 200, "hu": "true" }
    }
    if (ret1.rows[0].hu == 1 && userid == ret1.rows[0].userid) {
      ctx.body = { "status": 200, "hu": "true" }
    }
    if (ret1.rows[0].hu == 2 && concernid == ret1.rows[0].userid) {
      ctx.body = { "status": 200, "hu": "true" }
    }


  }
})

//关注和取消关注
router.post("/hu", async (ctx, next) => {
  let type = ctx.query.type
  let userid = ctx.request.body.userid
  let concernid = ctx.request.body.concernid

  //粉丝列表
  if (type == 1) {
    let sql1 = "select hu,id,userid from hs.concern where (userid=$1 AND concernid=$2) OR (userid=$3 AND concernid=$4)"
    let args = [userid, concernid, concernid, userid]
    let ret1 = await db.query(sql1, args);

    if (ret1.rows[0].hu == 1 || ret1.rows[0].hu == 2) {
      let sql2 = "update hs.concern SET hu=0 where id=$1"
      let args = [ret1.rows[0].id]
      let ret2 = await db.query(sql2, args)
      if (ret2.rowCount > 0) {
        let sql31 = "update hs.user set fannum=fannum+1 where id=$1"
        let args31 = [concernid]
        let ret31 = await db.query(sql31, args31)
        let sql32 = "update hs.user set concernnum=concernnum+1 where id=$1"
        let args32 = [userid]
        let ret32 = await db.query(sql32, args32)
        if (ret31.rowCount > 0 && ret32.rowCount > 0) {

          ctx.body = { "status": 200 }
        }


      }
    }
    if (ret1.rows[0].hu == 0) {
      if (ret1.rows[0].userid == userid) {
        let sql3 = "update hs.concern SET hu=2 where id=$1"
        let args = [ret1.rows[0].id]
        let ret3 = await db.query(sql3, args)
        if (ret3.rowCount > 0) {
          let sql31 = "update hs.user set fannum=fannum-1 where id=$1"
          let args31 = [concernid]
          let ret31 = await db.query(sql31, args31)
          let sql32 = "update hs.user set concernnum=concernnum-1 where id=$1"
          let args32 = [userid]
          let ret32 = await db.query(sql32, args32)
          if (ret31.rowCount > 0 && ret32.rowCount > 0) {

            ctx.body = { "status": 200 }
          }

        }
      }
      else {
        let sql4 = "update hs.concern SET hu=1 where id=$1"
        let args = [ret1.rows[0].id]
        let ret4 = await db.query(sql4, args)
        if (ret4.rowCount > 0) {
          let sql31 = "update hs.user set fannum=fannum-1 where id=$1"
          let args31 = [concernid]
          let ret31 = await db.query(sql31, args31)
          let sql32 = "update hs.user set concernnum=concernnum-1 where id=$1"
          let args32 = [userid]
          let ret32 = await db.query(sql32, args32)
          if (ret31.rowCount > 0 && ret32.rowCount > 0) {
            ctx.body = { "status": 200 }
          }
        }
      }
    }
  }

  //关注列表
  if (type == 2) {
    let sql1 = "select hu,id,userid from hs.concern where (userid=$1 AND concernid=$2) OR (userid=$3 AND concernid=$4)"
    let args = [userid, concernid, concernid, userid]
    let ret1 = await db.query(sql1, args);

    if (ret1.rows[0].hu == 1 || ret1.rows[0].hu == 2) {
      let sql2 = "delete from hs.concern where id=$1"
      let args = [ret1.rows[0].id]
      let ret2 = await db.query(sql2, args)
      if (ret2.rowCount > 0) {
        let sql31 = "update hs.user set fannum=fannum-1 where id=$1"
        let args31 = [concernid]
        let ret31 = await db.query(sql31, args31)
        let sql32 = "update hs.user set concernnum=concernnum-1 where id=$1"
        let args32 = [userid]
        let ret32 = await db.query(sql32, args32)
        if (ret31.rowCount > 0 && ret32.rowCount > 0) {

          ctx.body = { "status": 200 }
        }
      }
    }
    if (ret1.rows[0].hu == 0) {
      if (ret1.rows[0].userid == userid) {
        let sql3 = "update hs.concern SET hu=2 where id=$1"
        let args = [ret1.rows[0].id]
        let ret3 = await db.query(sql3, args)
        if (ret3.rowCount > 0) {
          let sql31 = "update hs.user set fannum=fannum-1 where id=$1"
          let args31 = [concernid]
          let ret31 = await db.query(sql31, args31)
          let sql32 = "update hs.user set concernnum=concernnum-1 where id=$1"
          let args32 = [userid]
          let ret32 = await db.query(sql32, args32)
          if (ret31.rowCount > 0 && ret32.rowCount > 0) {

            ctx.body = { "status": 200 }
          }
        }
      }
      else {
        let sql4 = "update hs.concern SET hu=1 where id=$1"
        let args = [ret1.rows[0].id]
        let ret4 = await db.query(sql4, args)
        if (ret4.rowCount > 0) {
          let sql31 = "update hs.user set fannum=fannum-1 where id=$1"
          let args31 = [concernid]
          let ret31 = await db.query(sql31, args31)
          let sql32 = "update hs.user set concernnum=concernnum-1 where id=$1"
          let args32 = [userid]
          let ret32 = await db.query(sql32, args32)
          if (ret31.rowCount > 0 && ret32.rowCount > 0) {

            ctx.body = { "status": 200 }
          }
        }
      }
    }

  }

  //页面点进去的关注
  if (type == 3) {
    let sql1 = "select hu,id,userid from hs.concern where (userid=$1 AND concernid=$2) OR (userid=$3 AND concernid=$4)"
    let args = [userid, concernid, concernid, userid]
    let ret1 = await db.query(sql1, args);

    if (ret1.rowCount <= 0) {
      let sql1 = "insert into hs.concern (id,userid,concernid,hu) values ($1,$2,$3,$4)"
      let date = String(Date.now()).substring(7)
      let args = [date, userid, concernid, 1]
      let ret2 = await db.query(sql1, args)
      if (ret2.rowCount > 0) {
        console.log(1)
        let sql31 = "update hs.user set fannum=fannum+1 where id=$1"
        let args31 = [concernid]
        let ret31 = await db.query(sql31, args31)
        let sql32 = "update hs.user set concernnum=concernnum+1 where id=$1"
        let args32 = [userid]
        let ret32 = await db.query(sql32, args32)
        if (ret31.rowCount > 0 && ret32.rowCount > 0) {

          ctx.body = { "status": 200 }
        }
      }

    }
    if (ret1.rowCount > 0) {
      if (ret1.rows[0].hu == 1 && userid == ret1.rows[0].userid) {
        let sql2 = "delete from hs.concern where id=$1"
        let args = [ret1.rows[0].id]
        let ret2 = await db.query(sql2, args)
        if (ret2.rowCount > 0) {
          let sql31 = "update hs.user set fannum=fannum-1 where id=$1"
          let args31 = [concernid]
          let ret31 = await db.query(sql31, args31)
          let sql32 = "update hs.user set concernnum=concernnum-1 where id=$1"
          let args32 = [userid]
          let ret32 = await db.query(sql32, args32)
          if (ret31.rowCount > 0 && ret32.rowCount > 0) {

            ctx.body = { "status": 200 }
          }
        }
      }
      if (ret1.rows[0].hu == 1 && concernid == ret1.rows[0].userid) {
        let sql2 = "update hs.concern set hu=0 where id=$1"
        let args = [ret1.rows[0].id]
        let ret2 = await db.query(sql2, args)
        if (ret2.rowCount > 0) {
          let sql31 = "update hs.user set fannum=fannum+1 where id=$1"
          let args31 = [concernid]
          let ret31 = await db.query(sql31, args31)
          let sql32 = "update hs.user set concernnum=concernnum+1 where id=$1"
          let args32 = [userid]
          let ret32 = await db.query(sql32, args32)
          if (ret31.rowCount > 0 && ret32.rowCount > 0) {

            ctx.body = { "status": 200 }
          }
        }
      }
      if (ret1.rows[0].hu == 0) {
        if (ret1.rows[0].userid == userid) {
          let sql3 = "update hs.concern SET hu=2 where id=$1"
          let args = [ret1.rows[0].id]
          let ret3 = await db.query(sql3, args)
          if (ret3.rowCount > 0) {
            let sql31 = "update hs.user set fannum=fannum-1 where id=$1"
            let args31 = [concernid]
            let ret31 = await db.query(sql31, args31)
            let sql32 = "update hs.user set concernnum=concernnum-1 where id=$1"
            let args32 = [userid]
            let ret32 = await db.query(sql32, args32)
            if (ret31.rowCount > 0 && ret32.rowCount > 0) {

              ctx.body = { "status": 200 }
            }
          }
        }
        else {
          let sql4 = "update hs.concern SET hu=1 where id=$1"
          let args = [ret1.rows[0].id]
          let ret4 = await db.query(sql4, args)
          if (ret4.rowCount > 0) {
            let sql31 = "update hs.user set fannum=fannum-1 where id=$1"
            let args31 = [concernid]
            let ret31 = await db.query(sql31, args31)
            let sql32 = "update hs.user set concernnum=concernnum-1 where id=$1"
            let args32 = [userid]
            let ret32 = await db.query(sql32, args32)
            if (ret31.rowCount > 0 && ret32.rowCount > 0) {

              ctx.body = { "status": 200 }
            }
          }
        }
      }
      if (ret1.rows[0].hu == 2 && userid == ret1.rows[0].userid) {
        let sql2 = "update hs.concern set hu=0 where id=$1"
        let args = [ret1.rows[0].id]
        let ret2 = await db.query(sql2, args)
        if (ret2.rowCount > 0) {
          let sql31 = "update hs.user set fannum=fannum+1 where id=$1"
          let args31 = [concernid]
          let ret31 = await db.query(sql31, args31)
          let sql32 = "update hs.user set concernnum=concernnum+1 where id=$1"
          let args32 = [userid]
          let ret32 = await db.query(sql32, args32)
          if (ret31.rowCount > 0 && ret32.rowCount > 0) {

            ctx.body = { "status": 200 }
          }
        }
      }
      if (ret1.rows[0].hu == 2 && concernid == ret1.rows[0].userid) {
        let sql2 = "delete from hs.concern where id=$1"
        let args = [ret1.rows[0].id]
        let ret2 = await db.query(sql2, args)
        if (ret2.rowCount > 0) {
          let sql31 = "update hs.user set fannum=fannum-1 where id=$1"
          let args31 = [concernid]
          let ret31 = await db.query(sql31, args31)
          let sql32 = "update hs.user set concernnum=concernnum-1 where id=$1"
          let args32 = [userid]
          let ret32 = await db.query(sql32, args32)
          if (ret31.rowCount > 0 && ret32.rowCount > 0) {
            ctx.body = { "status": 200 }
          }
        }
      }


    }
  }



})

//粉丝和关注列表的查询


//最近观看的查询
router.post('/history', async (ctx, next) => {
  let content = []
  let history = JSON.parse(ctx.request.body.history)
  for (var i = 0; i < history.length; i++) {
    let sql1 = "select c.id,c.name,c.type,c.outline,u.name username,u.avatar,c.voicenum,c.perface from hs.card AS c,hs.user AS u where c.id=$1 and c.userid=u.id"
    let args = [history[i]]
    let ret1 = await db.query(sql1, args);
    // ret1.rows
    content.push(ret1.rows[0])
  }
  console.log(content)
  ctx.body = { "status": 200, "content": content }
});

//评论显示
router.post('/comment', async (ctx, next) => {
  let userid = ctx.request.body.userid
  // let sql1 = "select p.perface,array_agg(distinct p.perface order by p.perface,','), c.comments,u.name,u.avatar,w.id,c.date" +
  //   " from hs.works as w,hs.comments as c,hs.user as u,hs.picaddress as p " +
  //   "where w.userid=$1 and w.id=c.workid and w.id=p.workid and c.userid=u.id group by c.comments"
  let sql1 = "select distinct(p.perface),c.comments,u.name,u.avatar,w.id,c.date" +
    " from hs.works as w,hs.comments as c,hs.user as u,hs.picaddress as p " +
    "where w.userid=$1 and w.id=c.workid and w.id=p.workid and c.userid=u.id order by c.date desc"
  let args = [userid]
  let ret1 = await db.query(sql1, args);
  console.log(ret1.rows)
  ctx.body = ret1.rows

})
module.exports = router
